import * as Axios from "axios";
import iView from 'view-design'

let baseURL: string = "https://api.github.com/repos";
const axios = Axios.default.create({
    baseURL, // api请求的baseURL
    timeout: 0,
    withCredentials: true, // 允许跨域 cookie
    headers: {
        "X-Requested-With": "XMLHttpRequest",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
    },
    maxContentLength: 2000,
    transformResponse: [
        (resData, resheader) => {
            let data: any;
            try {
                data = JSON.parse(resData);
            } catch (e) {
                data = JSON.parse(resData);
            }
            return data;
        }
    ]
});
axios.interceptors.request.use(config => {
    // @ts-ignore
    iView.LoadingBar.start();
    return config;
}, err => {
    // @ts-ignore
    iView.LoadingBar.error();
    return Promise.reject(err);
});
axios.interceptors.response.use((response): any => {
    if (response.data.success) {
        // @ts-ignore
        iView.LoadingBar.finish();
    } else {
        // @ts-ignore
        iView.LoadingBar.error();
        // @ts-ignore
        iView.Message.error(response.data.errorMessage || '未知错误');
    }
    return response;
}, err => {
    // @ts-ignore
    iView.LoadingBar.error()
    // @ts-ignore
    iView.Modal.error({
        closable: true,
        title: '请求失败',
        content: '网络异常或服务器异常...请稍后重试'
    });
    return Promise.reject(err);
})
export default axios

